//
//  MainViewViewController.swift
//  Example
//
//  Created by xx on 2025/3/24.
//

import UIKit
import FastSwiftSDK

class MainViewViewController: FastViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    
    private lazy var collectionView : UICollectionView = {
        // 设置 UICollectionViewFlowLayout
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal // 设置横向滚动
        layout.itemSize = CGSize(width: 150, height: 150) // 设置每个 cell 的大小
        layout.minimumLineSpacing = 10 // 设置 cell 之间的间距
        
        // 初始化 UICollectionView
        let    collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.backgroundColor = .xDDD
        collectionView.dataSource = self
        collectionView.delegate = self
        
        collectionView.register(MainCollectionViewCell.self, forCellWithReuseIdentifier: "customCell")
        
        self.view.addSubview(collectionView)
        return  collectionView
    } ()
    
    // MARK: - Data
    let items:Array<Dictionary<String,Any>> = [["title":"蜂窝灯","img":"gonggao","vc":LightViewController()],
                                               ["title":"灯带","img":"gonggao","vc":LightViewController()],
                                               ["title":"主灯","img":"gonggao","vc":LightViewController()],
                                               ["title":"风扇","img":"gonggao","vc":LightViewController()],
                                               ["title":"鱼缸","img":"shipin","vc":LightViewController()],
                                               ["title":"待办","img":"daka","vc":LightViewController()],
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .xDDD
    }
    override func viewWillAppear(_ animated: Bool) {
        XXDeviceManager.shared.refresh()
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        collectionView.snp.makeConstraints { make in
            make.top.left.equalTo(10)
            make.right.bottom.equalTo(-10)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MainCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! MainCollectionViewCell
        cell.setData(items[indexPath.item])
        return cell
    }
    
    
    // MARK: - UICollectionViewDelegate
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Selected item: \(items[indexPath.item])")
        let vc = items[indexPath.item].value("vc")
        push(vc as! UIViewController)
    }
    
}
